package com.sun.module_category.ui

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.material3.Button
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.PrimaryScrollableTabRow
import androidx.compose.material3.Tab
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import com.sun.module_base.app.ResultState
import com.sun.module_category.model.ParentBean
import kotlinx.coroutines.launch


/**
 *@author:sunc
 *@time :2025/7/29 16:57
 *@desc: 描述
 */


@OptIn(ExperimentalMaterial3Api::class)
@Preview(showBackground = true)
@Composable
fun CategoryScreen(modifier: Modifier = Modifier, viewModel: CategoryViewModel = hiltViewModel()) {


    val collectAsState = viewModel.categoryFlow.collectAsState()


    val scope = rememberCoroutineScope()

    Box(modifier = modifier.fillMaxSize()) {
        collectAsState.value.let {

            when (collectAsState.value) {
                is ResultState.Loading -> {
                    CircularProgressIndicator(
                        color = Color.White, modifier = Modifier.align(
                            Alignment.Center
                        )
                    )
                }

                is ResultState.Error -> {
                    Column(
                        modifier = Modifier.align(Alignment.Center)
                    ) {
                        Text(
                            text = "网络请求加载失败...",
                            fontWeight = FontWeight.Bold,
                            color = Color.White,
                            textAlign = TextAlign.Center,
                            fontSize = 25.sp
                        )
                        Button(onClick = { viewModel.getProjectCategory() }) {
                            Text("重试")
                        }
                    }
                }

                is ResultState.Success<MutableList<ParentBean>?> -> {
                    (collectAsState.value as ResultState.Success<MutableList<ParentBean>?>).data?.let {
                        val pagerState = rememberPagerState { it.size }
                        Column(modifier = Modifier.fillMaxSize()) {

                            PrimaryScrollableTabRow(selectedTabIndex = pagerState.currentPage) {
                                it.forEachIndexed { index, bean ->
                                    Tab(selected = index == pagerState.currentPage, onClick = {
                                        scope.launch {
                                            pagerState.animateScrollToPage(index)
                                        }
                                    }, text = { Text(bean.name ?: "未知") })
                                }
                            }
                            HorizontalDivider()

                            HorizontalPager(state = pagerState, userScrollEnabled = false) { page ->
//                                Text("列表页")
                                CategoryItemScreen(cid = it[page].id)
                            }
                        }
                    }
                }
            }
        }


    }
}
